package com.poetize.plus.entity.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.poetize.plus.entity.dao.CommentFloorDAO;
import com.poetize.plus.entity.po.User;
import com.poetize.plus.utils.TimeUtil;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zuosy
 * @created 2024/4/11 13:14
 */
public record CommentFloorVO(
        @Schema(description = "评论ID")
        Long id,

        @Schema(description = "楼层")
        Integer floor,

        @Schema(description = "评论人状态")
        Boolean isAdmin,

        @Schema(description = "评论人名称")
        String username,

        @Schema(description = "评论人头像")
        String avatar,

        @Schema(description = "评论内容")
        String commentContent,

        @Schema(description = "时间")
        String time,

        @Schema(description = "点赞数")
        Long likeCount,

        @Schema(description = "点踩数")
        Long dislikeCount,

        @Schema(description = "子评论数量")
        Long childCount,

        @Schema(description = "子评论")
        List<CommentFloorChild> child
) {

    public record CommentFloorChild(
            @Schema(description = "评论ID")
            Long id,

            @Schema(description = "评论人状态")
            Boolean isAdmin,

            @Schema(description = "评论人名称")
            String username,

            @Schema(description = "评论人头像")
            String avatar,

            @Schema(description = "父评论人")
            String parentUsername,

            @Schema(description = "评论内容")
            String commentContent,

            @Schema(description = "时间")
            String time,

            @Schema(description = "点赞数")
            Long likeCount,

            @Schema(description = "点踩数")
            Long dislikeCount
    ){

        /**
         * 评论信息和用户信息组装成评论子树
         * @param commentFloorDAO
         * @param user
         * @param parentUsername
         * @return
         */
        public static CommentFloorChild convertChild(CommentFloorDAO commentFloorDAO, User user, String parentUsername){
            return new CommentFloorChild(
                    commentFloorDAO.id(),
                    user.getRole().equals("administrator"),
                    user.getUsername(),
                    user.getAvatar(),
                    parentUsername,
                    commentFloorDAO.commentContent(),
                    TimeUtil.compute(commentFloorDAO.createTime()),
                    commentFloorDAO.likeCount(),
                    commentFloorDAO.dislikeCount());
        }

    }

    /**
     * DAO转VO
     * @param commentFloorDAO
     * @param user
     * @param child
     * @return
     */
    public static CommentFloorVO convertDAO(CommentFloorDAO commentFloorDAO, List<CommentFloorChild> child, User user, Long childCount) {
        return new CommentFloorVO(
                commentFloorDAO.id(),
                commentFloorDAO.floorNum(),
                user.getRole().equals("administrator"),
                user.getUsername(),
                user.getAvatar(),
                commentFloorDAO.commentContent(),
                TimeUtil.compute(commentFloorDAO.createTime()),
                commentFloorDAO.likeCount(),
                commentFloorDAO.dislikeCount(),
                childCount,
                child);
    }
}
